package  
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import assets.trayicon;
	import assets.visual
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fl.controls.ComboBox;
	import flash.events.KeyboardEvent;
	
	/**
	 * вьюха
	 * @author yaempechenki@gmail.com
	 */
	public class View extends Sprite
	{
		private var _visual:MovieClip = null;
		private var _portsList:ComboBox = null;
		public var port:String = "";
		public var uid:String = "";
		public function View() 
		{
			_visual = new visual();
			addChild(_visual);
			
			_visual.connectState.alpha = 0;
			_visual.addEventListener(MouseEvent.CLICK, onMouseClickHandler);
			
			_portsList = _visual.portList;
			_portsList.addEventListener(Event.CHANGE, comboBoxChange);
		}
		
		public function showPorts(ports:Array):void {
			for (var i:int = 0; i < ports.length ; i++) 
			{
				_portsList.addItem({label:ports[i],data:ports[i]});
			}
		}
		
		public function somenameOfMethod():void 
		{
			//_visual.animateMove.play();
		}
		
		public function set connectionState (state:Boolean):void {
			_visual.connectState.alpha = 1;
			if (state) {
				_visual.connectState.gotoAndStop(2);
			} else {
				_visual.connectState.gotoAndStop(4);
			}
		}
		
		private function onMouseClickHandler(event:MouseEvent):void {
			var now:String = event.target.name;
			trace(now);
			if (now  == "goTrayBtn") {
				dispatchEvent(new Event(AppEvent.APP_TO_TRAY));
			}
			if (now  == "closeBtn") {
				dispatchEvent(new Event(AppEvent.APP_CLOSE));
			}
			if (now  == "offBtn") {
				dispatchEvent(new Event(AppEvent.APP_COMMAND_OFF));
			}
			if (now  == "onBtn") {
				dispatchEvent(new Event(AppEvent.APP_COMMAND_ON));
			}
		}
		
		private function comboBoxChange(event:Event):void {
			var selected:Object = event.target.selectedItem;
			port = selected.data;
			dispatchEvent(new Event(AppEvent.APP_PORT_SELECT));
			_portsList.enabled = false;
		}
		
	}
}